{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a254cc8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting langchain\n",
      "  Using cached langchain-0.3.26-py3-none-any.whl.metadata (7.8 kB)\n",
      "Collecting langchain-core<1.0.0,>=0.3.66 (from langchain)\n",
      "  Using cached langchain_core-0.3.68-py3-none-any.whl.metadata (5.8 kB)\n",
      "Collecting langchain-text-splitters<1.0.0,>=0.3.8 (from langchain)\n",
      "  Using cached langchain_text_splitters-0.3.8-py3-none-any.whl.metadata (1.9 kB)\n",
      "Collecting langsmith>=0.1.17 (from langchain)\n",
      "  Using cached langsmith-0.4.4-py3-none-any.whl.metadata (15 kB)\n",
      "Collecting pydantic<3.0.0,>=2.7.4 (from langchain)\n",
      "  Using cached pydantic-2.11.7-py3-none-any.whl.metadata (67 kB)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (2.0.35)\n",
      "Requirement already satisfied: requests<3,>=2 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (2.32.3)\n",
      "Requirement already satisfied: PyYAML>=5.3 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (6.0.2)\n",
      "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain) (4.0.3)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain) (8.5.0)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain) (1.33)\n",
      "Collecting packaging<25,>=23.2 (from langchain-core<1.0.0,>=0.3.66->langchain)\n",
      "  Using cached packaging-24.2-py3-none-any.whl.metadata (3.2 kB)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.66->langchain) (4.14.1)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.66->langchain) (3.0.0)\n",
      "Collecting annotated-types>=0.6.0 (from pydantic<3.0.0,>=2.7.4->langchain)\n",
      "  Using cached annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n",
      "Collecting pydantic-core==2.33.2 (from pydantic<3.0.0,>=2.7.4->langchain)\n",
      "  Downloading pydantic_core-2.33.2-cp310-cp310-win_amd64.whl.metadata (6.9 kB)\n",
      "Collecting typing-inspection>=0.4.0 (from pydantic<3.0.0,>=2.7.4->langchain)\n",
      "  Using cached typing_inspection-0.4.1-py3-none-any.whl.metadata (2.6 kB)\n",
      "Collecting charset-normalizer<4,>=2 (from requests<3,>=2->langchain)\n",
      "  Downloading charset_normalizer-3.4.2-cp310-cp310-win_amd64.whl.metadata (36 kB)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from requests<3,>=2->langchain) (3.10)\n",
      "Collecting urllib3<3,>=1.21.1 (from requests<3,>=2->langchain)\n",
      "  Using cached urllib3-2.5.0-py3-none-any.whl.metadata (6.5 kB)\n",
      "Collecting certifi>=2017.4.17 (from requests<3,>=2->langchain)\n",
      "  Using cached certifi-2025.6.15-py3-none-any.whl.metadata (2.4 kB)\n",
      "Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (3.1.0)\n",
      "Collecting httpx<1,>=0.23.0 (from langsmith>=0.1.17->langchain)\n",
      "  Using cached httpx-0.28.1-py3-none-any.whl.metadata (7.1 kB)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langsmith>=0.1.17->langchain) (3.10.7)\n",
      "Collecting requests-toolbelt<2.0.0,>=1.0.0 (from langsmith>=0.1.17->langchain)\n",
      "  Using cached requests_toolbelt-1.0.0-py2.py3-none-any.whl.metadata (14 kB)\n",
      "Collecting zstandard<0.24.0,>=0.23.0 (from langsmith>=0.1.17->langchain)\n",
      "  Downloading zstandard-0.23.0-cp310-cp310-win_amd64.whl.metadata (3.0 kB)\n",
      "Collecting anyio (from httpx<1,>=0.23.0->langsmith>=0.1.17->langchain)\n",
      "  Using cached anyio-4.9.0-py3-none-any.whl.metadata (4.7 kB)\n",
      "Collecting httpcore==1.* (from httpx<1,>=0.23.0->langsmith>=0.1.17->langchain)\n",
      "  Using cached httpcore-1.0.9-py3-none-any.whl.metadata (21 kB)\n",
      "Collecting h11>=0.16 (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.1.17->langchain)\n",
      "  Using cached h11-0.16.0-py3-none-any.whl.metadata (8.3 kB)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from anyio->httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (1.2.2)\n",
      "Requirement already satisfied: sniffio>=1.1 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from anyio->httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (1.3.1)\n",
      "Using cached langchain-0.3.26-py3-none-any.whl (1.0 MB)\n",
      "Using cached langchain_core-0.3.68-py3-none-any.whl (441 kB)\n",
      "Using cached langchain_text_splitters-0.3.8-py3-none-any.whl (32 kB)\n",
      "Using cached packaging-24.2-py3-none-any.whl (65 kB)\n",
      "Using cached pydantic-2.11.7-py3-none-any.whl (444 kB)\n",
      "Downloading pydantic_core-2.33.2-cp310-cp310-win_amd64.whl (2.0 MB)\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.0 MB ? eta -:--:--\n",
      "   ----- ---------------------------------- 0.3/2.0 MB ? eta -:--:--\n",
      "   ---------------- ----------------------- 0.8/2.0 MB 2.0 MB/s eta 0:00:01\n",
      "   -------------------------------- ------- 1.6/2.0 MB 2.4 MB/s eta 0:00:01\n",
      "   ---------------------------------------- 2.0/2.0 MB 2.6 MB/s eta 0:00:00\n",
      "Downloading charset_normalizer-3.4.2-cp310-cp310-win_amd64.whl (105 kB)\n",
      "Using cached urllib3-2.5.0-py3-none-any.whl (129 kB)\n",
      "Using cached annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",
      "Using cached certifi-2025.6.15-py3-none-any.whl (157 kB)\n",
      "Using cached langsmith-0.4.4-py3-none-any.whl (367 kB)\n",
      "Using cached httpx-0.28.1-py3-none-any.whl (73 kB)\n",
      "Using cached httpcore-1.0.9-py3-none-any.whl (78 kB)\n",
      "Using cached requests_toolbelt-1.0.0-py2.py3-none-any.whl (54 kB)\n",
      "Downloading zstandard-0.23.0-cp310-cp310-win_amd64.whl (495 kB)\n",
      "Using cached h11-0.16.0-py3-none-any.whl (37 kB)\n",
      "Using cached typing_inspection-0.4.1-py3-none-any.whl (14 kB)\n",
      "Using cached anyio-4.9.0-py3-none-any.whl (100 kB)\n",
      "Installing collected packages: zstandard, urllib3, typing-inspection, pydantic-core, packaging, h11, charset-normalizer, certifi, anyio, annotated-types, pydantic, httpcore, requests-toolbelt, httpx, langsmith, langchain-core, langchain-text-splitters, langchain\n",
      "\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "  Attempting uninstall: packaging\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "    Found existing installation: packaging 25.0\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "    Uninstalling packaging-25.0:\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "      Successfully uninstalled packaging-25.0\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "  Attempting uninstall: h11\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "    Found existing installation: h11 0.14.0\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "    Uninstalling h11-0.14.0:\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "      Successfully uninstalled h11-0.14.0\n",
      "   ---- -----------------------------------  2/18 [typing-inspection]\n",
      "   ----------- ----------------------------  5/18 [h11]\n",
      "   ----------------- ----------------------  8/18 [anyio]\n",
      "   ---------------------- ----------------- 10/18 [pydantic]\n",
      "   ---------------------- ----------------- 10/18 [pydantic]\n",
      "   ------------------------ --------------- 11/18 [httpcore]\n",
      "   ---------------------------- ----------- 13/18 [httpx]\n",
      "   ------------------------------- -------- 14/18 [langsmith]\n",
      "   --------------------------------- ------ 15/18 [langchain-core]\n",
      "   --------------------------------- ------ 15/18 [langchain-core]\n",
      "   --------------------------------- ------ 15/18 [langchain-core]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ------------------------------------- -- 17/18 [langchain]\n",
      "   ---------------------------------------- 18/18 [langchain]\n",
      "\n",
      "Successfully installed annotated-types-0.7.0 anyio-4.9.0 certifi-2025.6.15 charset-normalizer-3.4.2 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 langchain-0.3.26 langchain-core-0.3.68 langchain-text-splitters-0.3.8 langsmith-0.4.4 packaging-24.2 pydantic-2.11.7 pydantic-core-2.33.2 requests-toolbelt-1.0.0 typing-inspection-0.4.1 urllib3-2.5.0 zstandard-0.23.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
      "dask 2024.9.0 requires click>=8.1, which is not installed.\n",
      "dask-expr 1.1.14 requires pandas>=2, which is not installed.\n",
      "dask-expr 1.1.14 requires pyarrow>=14.0.1, which is not installed.\n",
      "fastparquet 2024.5.0 requires pandas>=1.5.0, which is not installed.\n",
      "graphrag 0.2.1 requires aiofiles<25.0.0,>=24.1.0, which is not installed.\n",
      "graphrag 0.2.1 requires azure-storage-blob<13.0.0,>=12.19.0, which is not installed.\n",
      "graphrag 0.2.1 requires nltk==3.8.1, which is not installed.\n",
      "graphrag 0.2.1 requires openai<2.0.0,>=1.37.1, which is not installed.\n",
      "graphrag 0.2.1 requires python-dotenv<2.0.0,>=1.0.0, which is not installed.\n",
      "graphrag 0.2.1 requires rich<14.0.0,>=13.6.0, which is not installed.\n",
      "lancedb 0.11.0 requires attrs>=21.3.0, which is not installed.\n",
      "lancedb 0.11.0 requires cachetools, which is not installed.\n",
      "lancedb 0.11.0 requires tqdm>=4.27.0, which is not installed.\n",
      "matplotlib 3.9.2 requires pillow>=8, which is not installed.\n",
      "matplotlib 3.9.2 requires pyparsing>=2.3.1, which is not installed.\n",
      "pip-tools 7.4.1 requires click>=8, which is not installed.\n",
      "pydantic-settings 2.5.2 requires python-dotenv>=0.21.0, which is not installed.\n",
      "seaborn 0.13.2 requires pandas>=1.2, which is not installed.\n",
      "statsmodels 0.14.3 requires pandas!=2.1.0,>=1.4, which is not installed.\n",
      "swifter 1.4.0 requires pandas>=1.0.0, which is not installed.\n",
      "swifter 1.4.0 requires tqdm>=4.33.0, which is not installed.\n",
      "uvicorn 0.30.6 requires click>=7.0, which is not installed.\n",
      "ollama 0.3.3 requires httpx<0.28.0,>=0.27.0, but you have httpx 0.28.1 which is incompatible.\n"
     ]
    }
   ],
   "source": [
    "! pip install langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5436ba34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: langchain\n",
      "Version: 0.3.26\n",
      "Summary: Building applications with LLMs through composability\n",
      "Home-page: \n",
      "Author: \n",
      "Author-email: \n",
      "License: MIT\n",
      "Location: d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages\n",
      "Requires: async-timeout, langchain-core, langchain-text-splitters, langsmith, pydantic, PyYAML, requests, SQLAlchemy\n",
      "Required-by: \n"
     ]
    }
   ],
   "source": [
    "! pip show langchain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e6073c2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: python-dotenv in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (1.1.1)\n"
     ]
    }
   ],
   "source": [
    "! pip install python-dotenv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "29696b7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting openai\n",
      "  Using cached openai-1.93.0-py3-none-any.whl.metadata (29 kB)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai) (4.9.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from openai) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai) (0.28.1)\n",
      "Collecting jiter<1,>=0.4.0 (from openai)\n",
      "  Downloading jiter-0.10.0-cp310-cp310-win_amd64.whl.metadata (5.3 kB)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai) (2.11.7)\n",
      "Requirement already satisfied: sniffio in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from openai) (1.3.1)\n",
      "Collecting tqdm>4 (from openai)\n",
      "  Using cached tqdm-4.67.1-py3-none-any.whl.metadata (57 kB)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai) (4.14.1)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from anyio<5,>=3.5.0->openai) (1.2.2)\n",
      "Requirement already satisfied: idna>=2.8 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from anyio<5,>=3.5.0->openai) (3.10)\n",
      "Requirement already satisfied: certifi in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from httpx<1,>=0.23.0->openai) (2025.6.15)\n",
      "Requirement already satisfied: httpcore==1.* in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from httpx<1,>=0.23.0->openai) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.16.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1.9.0->openai) (2.33.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic<3,>=1.9.0->openai) (0.4.1)\n",
      "Requirement already satisfied: colorama in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from tqdm>4->openai) (0.4.6)\n",
      "Using cached openai-1.93.0-py3-none-any.whl (755 kB)\n",
      "Downloading jiter-0.10.0-cp310-cp310-win_amd64.whl (207 kB)\n",
      "Using cached tqdm-4.67.1-py3-none-any.whl (78 kB)\n",
      "Installing collected packages: tqdm, jiter, openai\n",
      "\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   -------------------------- ------------- 2/3 [openai]\n",
      "   ---------------------------------------- 3/3 [openai]\n",
      "\n",
      "Successfully installed jiter-0.10.0 openai-1.93.0 tqdm-4.67.1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
      "graphrag 0.2.1 requires aiofiles<25.0.0,>=24.1.0, which is not installed.\n",
      "graphrag 0.2.1 requires azure-storage-blob<13.0.0,>=12.19.0, which is not installed.\n",
      "graphrag 0.2.1 requires nltk==3.8.1, which is not installed.\n",
      "graphrag 0.2.1 requires rich<14.0.0,>=13.6.0, which is not installed.\n",
      "lancedb 0.11.0 requires attrs>=21.3.0, which is not installed.\n",
      "lancedb 0.11.0 requires cachetools, which is not installed.\n",
      "swifter 1.4.0 requires pandas>=1.0.0, which is not installed.\n"
     ]
    }
   ],
   "source": [
    "! pip install openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "00c56583",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "111\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv \n",
    "load_dotenv(override=True)\n",
    "\n",
    "DeepSeek_API_KEY = os.getenv(\"DEEPSEEK_API_KEY\")\n",
    "print(111)  # 可以通过打印查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f81e6978",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！我是一个乐于助人的AI助手，随时准备为你提供帮助。我可以回答各种问题、提供建议、协助完成任务，或者陪你聊天解闷。无论是学习、工作还是生活中的疑问，我都会尽力给出最有用的信息。我没有实体存在，但24小时在线，随时为你服务。 \n",
      "\n",
      "我的知识截止到2023年，擅长处理文本信息，可以帮助写作、翻译、编程、数据分析等多种任务。不过要注意，我的回答仅供参考，重要决策还是需要你自己判断哦~ \n",
      "\n",
      "有什么我可以帮你的吗？\n"
     ]
    }
   ],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "# 初始化DeepSeek的API客户端\n",
    "client = OpenAI(api_key=DeepSeek_API_KEY, base_url=\"https://api.deepseek.com\")\n",
    "\n",
    "# 调用DeepSeek的API，生成回答\n",
    "response = client.chat.completions.create(\n",
    "    model=\"deepseek-chat\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": \"你是乐于助人的助手，请根据用户的问题给出回答\"},\n",
    "        {\"role\": \"user\", \"content\": \"你好，请你介绍一下你自己。\"},\n",
    "    ],\n",
    ")\n",
    "\n",
    "# 打印模型最终的响应结果\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "91f37dbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting langchain-deepseek\n",
      "  Downloading langchain_deepseek-0.1.3-py3-none-any.whl.metadata (1.1 kB)\n",
      "Requirement already satisfied: langchain-core<1.0.0,>=0.3.47 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-deepseek) (0.3.68)\n",
      "Collecting langchain-openai<1.0.0,>=0.3.9 (from langchain-deepseek)\n",
      "  Downloading langchain_openai-0.3.27-py3-none-any.whl.metadata (2.3 kB)\n",
      "Requirement already satisfied: langsmith>=0.3.45 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (0.4.4)\n",
      "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (8.5.0)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (1.33)\n",
      "Requirement already satisfied: PyYAML>=5.3 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (6.0.2)\n",
      "Requirement already satisfied: packaging<25,>=23.2 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (24.2)\n",
      "Requirement already satisfied: typing-extensions>=4.7 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (4.14.1)\n",
      "Requirement already satisfied: pydantic>=2.7.4 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (2.11.7)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (3.0.0)\n",
      "Requirement already satisfied: openai<2.0.0,>=1.86.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (1.93.0)\n",
      "Requirement already satisfied: tiktoken<1,>=0.7 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (0.7.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (4.9.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (0.10.0)\n",
      "Requirement already satisfied: sniffio in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (1.3.1)\n",
      "Requirement already satisfied: tqdm>4 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (4.67.1)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (1.2.2)\n",
      "Requirement already satisfied: idna>=2.8 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (3.10)\n",
      "Requirement already satisfied: certifi in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (2025.6.15)\n",
      "Requirement already satisfied: httpcore==1.* in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (0.16.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (2.33.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from pydantic>=2.7.4->langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (0.4.1)\n",
      "Requirement already satisfied: regex>=2022.1.18 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from tiktoken<1,>=0.7->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (2024.9.11)\n",
      "Requirement already satisfied: requests>=2.26.0 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from tiktoken<1,>=0.7->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (2.32.3)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (3.10.7)\n",
      "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (1.0.0)\n",
      "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from langsmith>=0.3.45->langchain-core<1.0.0,>=0.3.47->langchain-deepseek) (0.23.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (3.4.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in d:\\users\\sunshine\\anaconda3\\envs\\langchain\\lib\\site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (2.5.0)\n",
      "Requirement already satisfied: colorama in c:\\users\\sunshine\\appdata\\roaming\\python\\python310\\site-packages (from tqdm>4->openai<2.0.0,>=1.86.0->langchain-openai<1.0.0,>=0.3.9->langchain-deepseek) (0.4.6)\n",
      "Downloading langchain_deepseek-0.1.3-py3-none-any.whl (7.1 kB)\n",
      "Downloading langchain_openai-0.3.27-py3-none-any.whl (70 kB)\n",
      "Installing collected packages: langchain-openai, langchain-deepseek\n",
      "\n",
      "   ---------------------------------------- 2/2 [langchain-deepseek]\n",
      "\n",
      "Successfully installed langchain-deepseek-0.1.3 langchain-openai-0.3.27\n"
     ]
    }
   ],
   "source": [
    "! pip install langchain-deepseek"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "98acc653",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "think:嗯，用户让我介绍一下自己，这是一个很常见的开场问题。用户可能是第一次接触我，想快速了解我的能力和边界，也可能是想测试我的反应。  \n",
      "\n",
      "从简单的问候语来看，用户态度比较友好，没有特别具体的需求指向。这时候需要做一个既全面又简洁的自我介绍，重点突出三个层次：身份背景（让用户知道我是谁）、核心能力（解决什么问题）、使用边界（避免后续误会）。  \n",
      "\n",
      "用户可能更关心实际功能而非技术细节，所以要把“能做什么”说得具体些，比如直接举例“写代码/改简历”这种场景。同时要主动提及文件处理这类实用功能——很多人不知道AI能读文件。  \n",
      "\n",
      "最后用开放性问题收尾很重要，用户如果只是客套提问，可以自然转入真实需求；如果是测试型用户，反问能引导对话深化。表情符号选通用的微笑😊，避免过度热情。  \n",
      "\n",
      "对了，还得悄悄植入“免费”这个信息，现在很多AI工具开始收费了，用户会担心突然弹支付页面吧。\n",
      "answer:你好呀！😊 很高兴认识你～  \n",
      "\n",
      "我是 **DeepSeek-R1**，由中国的人工智能公司「深度求索（DeepSeek）」研发的一款智能助手。我的使命就是用清晰、温暖又靠谱的方式，帮你解答问题、整理资料、学习知识、激发灵感，成为你生活中贴心又强大的“知识搭子”✨  \n",
      "\n",
      "我擅长的事情可不少哦，比如：  \n",
      "🔍 **答疑解惑**：学习上的难题、生活中的冷知识、工作中的概念……有问必答！  \n",
      "📚 **文本处理**：写文章、改简历、润色邮件、起标题、写小说、生成报告，通通不在话下～  \n",
      "📊 **文件分析**：支持上传 PDF、Word、Excel、PPT 等文档，我可以帮你总结重点、提取信息、翻译内容等。  \n",
      "🧠 **逻辑推理**：数学题、编程题、逻辑谜题，我都能一步步分析推理，带你找到答案。  \n",
      "🌐 **联网搜索（官方网页/App内）**：可以获取最新信息（比如新闻、百科、实时数据等）。  \n",
      "\n",
      "目前我是 **完全免费的**🎉，没有语音功能，但支持超长上下文（最多128K tokens），你发一大段资料我也能轻松读懂～  \n",
      "\n",
      "无论你是学生、上班族、创作者，还是只是好奇想聊天，我都在这儿等你！  \n",
      "现在，有什么我可以帮你的吗？😊\n"
     ]
    }
   ],
   "source": [
    "from langchain.chat_models import init_chat_model\n",
    "\n",
    "model = init_chat_model(model=\"deepseek-reasoner\", model_provider=\"deepseek\")  \n",
    "\n",
    "question = \"你好，请你介绍一下你自己。\"\n",
    "\n",
    "\n",
    "result = model.invoke(question)\n",
    "p = result.additional_kwargs['reasoning_content']\n",
    "print(\"think:\"+p)\n",
    "print(\"answer:\"+result.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6451e666",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "answer:你好！我是 **DeepSeek Chat**，由深度求索（DeepSeek）公司开发的智能 AI 助手。我可以帮助你解答各种问题，包括学习、工作、编程、写作、生活建议等多个领域。  \n",
      "\n",
      "### **我的特点**：  \n",
      "✅ **免费使用**：目前无需付费，可以随时向我提问！  \n",
      "✅ **知识丰富**：我的知识截止到 **2024年7月**，可以为你提供最新的信息。  \n",
      "✅ **超长上下文**：支持 **128K** 上下文记忆，能处理超长文档和复杂对话。  \n",
      "✅ **文件阅读**：可以上传 **PDF、Word、Excel、PPT、TXT** 等文件，并帮你提取和分析内容。  \n",
      "✅ **中文优化**：对中文理解优秀，适合处理各种中文任务。  \n",
      "\n",
      "### **我能帮你做什么？**  \n",
      "📖 **学习辅导**：数学、物理、编程、论文写作……  \n",
      "💼 **工作助手**：写邮件、做PPT、分析数据、优化简历……  \n",
      "💡 **创意灵感**：写故事、起名字、头脑风暴……  \n",
      "📊 **代码编程**：Python、Java、C++、SQL……  \n",
      "🌍 **生活百科**：旅行建议、美食推荐、健康小贴士……  \n",
      "\n",
      "你可以随时向我提问，我会尽力提供最准确、有用的回答！😊 有什么我可以帮你的吗？\n"
     ]
    }
   ],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain.chat_models import init_chat_model\n",
    "\n",
    "# 使用 DeepSeek 模型\n",
    "model = init_chat_model(model=\"deepseek-chat\", model_provider=\"deepseek\")  \n",
    "\n",
    "# 直接使用模型 + 输出解析器搭建一个链\n",
    "basic_qa_chain = model | StrOutputParser()\n",
    "\n",
    "# 查看输出结果\n",
    "question = \"你好，请你介绍一下你自己。\"\n",
    "result = basic_qa_chain.invoke(question)\n",
    "print(\"answer:\"+result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "556be049",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "answer:no\n"
     ]
    }
   ],
   "source": [
    "from langchain.output_parsers.boolean import BooleanOutputParser\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "\n",
    "prompt_template = ChatPromptTemplate([\n",
    "    (\"system\", \"你是一个乐意助人的助手，请根据用户的问题给出回答\"),\n",
    "    (\"user\", \"这是用户的问题： {topic}， 请用 yes 或 no 来回答\")\n",
    "])\n",
    "\n",
    "# 直接使用模型 + 输出解析器\n",
    "bool_qa_chain = prompt_template | model | StrOutputParser()\n",
    "# 测试\n",
    "question = \"请问 1 + 1 是否 大于 2？\"\n",
    "result = bool_qa_chain.invoke(question)\n",
    "print(\"answer:\"+result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
